<?php
namespace app\api\controller\haipi;

use app\common\controller\Frontend;
use app\common\model\haipi\Discuz as DiscuzModel; 
/**
 * 帖子
 */
class Post extends Frontend {
    public function index() {
        $page = $this->request->get("page",1);
        $pagesize = $this->request->get("pagesize",10);
        $auditStatus = $this->request->get('audit_status', null);
        $showopage = $page == 1 ? true : false;

        $list = DiscuzModel::query()
        ->where('user_id', $this->auth->id)
            ->order('id desc')
            ->where(function($query)use($auditStatus){
                if($auditStatus !== null){
                    $query->where('audit_status', $auditStatus);
                }
            }) 
            ->field(['id', 'title', 'thumb_image', 'publish_time', 'audit_status', 'status'])
            ->paginate([
                'list_rows'=> $pagesize,
                'page' => $page,
            ], !$showopage);
                      
        $data = [
            'items' => $list->items()
        ];
      
        if($showopage){
            $data['pageInfo']= [
                'total' => $list->total(),
                'totalPage' => $list->lastPage(),
                'currentPage' => $page,
            ];
        }
        $this->success('ok', $data);
    }

    public function add()  {
        $data = $this->request->post();
        $validate = new \app\api\validate\haipi\Post();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $data['thumb_image'] = array_shift($data['images']);
        $data['audit_status'] = DiscuzModel::AUDIT_STATUS_DOING;
        $data['publish_time'] = date('Y-m-d H:i:s', time());
        $data['source'] = DiscuzModel::SOURCE_TYPE_SELF;
        $data['status'] = DiscuzModel::STATUS_DISABLE;
        $data['user_id'] = $this->auth->id;
        DiscuzModel::create($data);
        $this->success('ok');
    }

    public function edit(){
        $data = $this->request->post();
        $validate = new \app\api\validate\haipi\Post();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
     
      
        $data['thumb_image'] = array_shift($data['images']);
        $data['audit_status'] = DiscuzModel::AUDIT_STATUS_DOING;
        $data['source'] = DiscuzModel::SOURCE_TYPE_SELF;
        $data['status'] = DiscuzModel::STATUS_DISABLE;
        DiscuzModel::update($data);
        $this->success('ok');
    }

    public function del(){
        $id = $this->request->post('id');
        if(!$id){   
            $this->error("参数不正确");
        }
        DiscuzModel::query()
        ->where("id", $id)
        ->where('user_id', $this->auth->id)
        ->delete();
        $this->success('ok'); 
    }

    public function read() {
        $id = $this->request->get('id');
        $data = DiscuzModel::query()
            ->where("id", $id)
            ->append(['areas_text'])
            ->where('user_id', $this->auth->id)
            ->find();
        if(!$data){ 
            $this->error('贴子不存在');
        }
        $data['images'] = [$data['thumb_image'], ...$data['images']] ;
        $this->success('ok', $data);
    }
}